package com.egov.config.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * Description: http请求处理切面
 *
 * @Author: denggang
 * @Date: 2018/7/16 11:12
 */
@Slf4j
@Aspect
@Component
public class LogAspect {

    private Long requestTime;

    /**
     * 切入点，表达式写法自行百度
     */
    @Pointcut("execution(public * com.egov.business.*.controller..*.*(..))")
    public void logPointCut() {
    }

    /**
     * controller方法执行之前执行，请求参数记录
     */
    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //用户信息
//        User user = (User) request.getSession().getAttribute("user");
//        if (user != null) {
//            logSys.setUser(user.getLoginName());
//        } else {
//            logSys.setUser("null");
//        }
//
//        //初始化请求时间
//        requestTime = TimeUtil.getCurrentTimeMillisecond();

//        logSys.setRequestTime(new Date());

        //url
            log.info("url={}", request.getRequestURL());
//        logSys.setUrl(request.getRequestURL().toString());

        //method(POST/GET ....)
        log.info("method={}", request.getMethod());
//        logSys.setMethod(request.getMethod());

        //ip
        log.info("ip={}", request.getRemoteAddr());
//        logSys.setIp(request.getRemoteAddr());

        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        //类方法
        log.info("class_method={}", classMethod);
//        logSys.setClassMethod(classMethod);

        //参数
        log.info("args={}", joinPoint.getArgs());
//        logSys.setArgs(Arrays.toString(joinPoint.getArgs()));

    }

    /**
     * controller方法返回之后执行
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "object")
    public void doAfterReturning(Object object) {
        //获得请求返回时间
//        Long responseTime = TimeUtil.getCurrentTimeMillisecond();

        if (object != null) {
            //返回值打印
            log.info("response={}", object.toString());
        } else {
            //返回值打印
            log.info("response=null");

        }

//        logSys.setResponse("200");
        //请求耗时
//        log.info("Request time(millisecond)={}", responseTime - requestTime);
//        logSys.setSpendTime(new Long(responseTime - requestTime).intValue());

//        logSysMapper.insertSelective(log);
    }

    /**
     * 拦截异常，记录异常日志，并设置对应的异常信息
     * 目前只拦截Exception
     *
     * @param e 异常对象
     */
    @AfterThrowing(pointcut = "logPointCut()", throwing = "e")
    public void handle(JoinPoint point, Exception e) {
//        logSys.setResponse("500");
//        logSys.setException("异常信息：" + e.getMessage());
//        logSysMapper.insertSelective(log);
    }

}